package io.github.talelin.latticy.service.K;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.K.CollectorDTO;
import io.github.talelin.latticy.mapper.K.CollectorMapper;
import io.github.talelin.latticy.model.K.CollectordwmcDO;
import io.github.talelin.latticy.model.K.CollectorwgmcDO;
import io.github.talelin.latticy.model.K.CollectorDO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author generator@TaleLin
 * @since 2021-06-03
 */
@Service
public class CollectorService extends ServiceImpl<CollectorMapper, CollectorDO> {

    @Autowired
    private CollectorMapper CollectorMapper;

    public CollectorDO getById(Integer id) {
        CollectorDO b_gateway = CollectorMapper.selectById(id);
        return b_gateway;
    }

    public void update(CollectorDTO dto, Long id) {
        CollectorDO CollectorDO = this.getById(id);
        if (CollectorDO == null) {
            throw new NotFoundException(70000);
        }
        BeanUtils.copyProperties(dto, CollectorDO);
        this.updateById(CollectorDO);
    }

    public void delete(long id) {
        CollectorDO exist = this.getById(id);
        if (exist == null) {
            throw new NotFoundException(70000);
        }
        this.getBaseMapper().deleteById(id);
    }

    public IPage<CollectorDO> seachCollector(
            Page<CollectorDO> pager,
            String keyword){
        QueryWrapper<CollectorDO> wrapper = new QueryWrapper<>();
        wrapper.like("name",keyword)
                .or().like("location",keyword)
                .or().like("uuid",keyword);
        IPage<CollectorDO> iPage = CollectorMapper.selectPage(pager,wrapper);
        return iPage;
    }

    public List<CollectordwmcDO> getCollector(){

        return this.getBaseMapper().getCollector();
    }

    public List<CollectorwgmcDO> getCollector1(){

        return this.getBaseMapper().getCollector1();
    }
}
